<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>新增用户页面</title>
    <header th:replace="header::html"></header>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <div class="layui-form-item">
                <label for="username" class="layui-form-label">
                    <span class="x-red">*</span>登录名
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="username" name="username" required="" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>将会成为您唯一的登入名
                </div>
            </div>
            <div class="layui-form-item">
                <label for="telephone" class="layui-form-label">
                    <span class="x-red">*</span>手机
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="telephone" name="telephone" required="" lay-verify="telephone"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>将会成为您唯一的登入名
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_email" class="layui-form-label">
                    <span class="x-red">*</span>邮箱
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="L_email" name="email" required="" lay-verify="email"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>密码
                </label>
                <div class="layui-input-inline">
                    <input type="password" id="L_pass" name="password" required="" lay-verify="pass"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    6到16个字符
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_repass" class="layui-form-label">
                    <span class="x-red">*</span>确认密码
                </label>
                <div class="layui-input-inline">
                    <input type="password" id="L_repass" name="repass" required="" lay-verify="repass"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_email" class="layui-form-label">生日</label>
                <div  class="layui-input-inline" >
                    <input type="text" id="L_birthday"  name="birthday" placeholder="yyyy-MM-dd" class="layui-input"><!--th:value="${sysUser.birthday}"-->
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio"   name="sex" value="1" title="男" checked>
                    <input type="radio"   name="sex" value="0" title="女" ><!--th:value="${sysUser.sex}"-->
                </div>
            </div>
          <!--  <div class="layui-form-item">
                <label  class="layui-form-label">性别</label>
                <div  class="layui-input-block" >
                    <input type="radio"  th:value="${sysUser.sex}" name="sex" value="1" title="男" checked="" >
                    <input type="radio"  th:value="${sysUser.sex}" name="sex" value="0" title="女" >
                </div>
            </div>-->
            <div class="layui-form-item">
                <label class="layui-form-label">角色</label>
                <div class="layui-input-block show-role-container"></div>
            </div>
            <div class="layui-form-item">
                <label for="L_email" class="layui-form-label">
                </label>
                <button  class="layui-btn" lay-filter="add" lay-submit>
                    增加
                </button>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form', 'layer','laydate'],function() {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate;

        getAllRole(function () {
            layui.form.render('radio')
        });
        //日期
        laydate.render({
            elem: '#L_birthday'
            , format: 'yyyy-MM-dd'//可任意组合
        });
        //自定义验证规则
        form.verify({
            nikename: function (value) {
                if (value.length < 5) {
                    return '昵称至少得5个字符啊';
                }
            },
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            repass: function (value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });
        //监听提交
        form.on('submit(add)', function (data) {
            console.log(JSON.stringify(data.field));
            //设置角色不能为空判断
            var rolelRadio = $("[name=roleId]:checked");
            if (rolelRadio == undefined || rolelRadio.length <= 0) {
                layer.alert("请给该用户设置角色哟");
                return false;
            }

            data.field.roleId = rolelRadio.val();
            $.ajax({
                url: "/user/add",
                type: "POST",
                data: data.field,
                dataType: 'json',
                success:function(result){//检测返回的结果，下列为resultcode中的枚举
                    if (result.code == 500 || result.code == 5000100 || result.code == 5000101|| result.code == 5000102) {
                        layer.alert(result.msg);
                    } else {
                        layer.alert("设置成功", {icon: 6}, function () {
                            //关闭当前frame
                            xadmin.close();
                            //可以对父窗口进行刷新
                            xadmin.father_reload();
                        });
                    }
                }
            });
            return false;
        });
    });
        function getAllRole(callback) {//查询关于角色的数据
            $.ajax({
                url: "/role/all",
                type: "GET",
                dataType:'json',
                success: function (result) {
                    var html = "";
                    if (result.datas && result.datas.length > 0) {
                        for (i in result.datas) {
                            html += '<input type="radio" name="roleId" value="' + result.datas[i].id + '"title="' + result.datas[i].name + '">' +
                                '<div class = "layui-unselect layui-form-radio layui-form-radioed" > <i class="layui-anim layui-icon"></i><div>' + result.datas[i].name + '</div></div>';
                        }
                        $(".show-role-container").html(html);
                        callback();
                    }
                }
            });
        }
</script>
<script>var _hmt = _hmt || []; (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
</body>

</html>
